package com.campus.controller.admin;

import com.campus.constant.JwtClaimsConstant;
import com.campus.pojo.dto.AdminLoginDTO;
import com.campus.pojo.vo.Admin;
import com.campus.properties.JwtProperties;
import com.campus.result.Result;
import com.campus.service.AdminService;
import com.campus.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@CrossOrigin //允许跨域请求
public class AdminController {

    private final AdminService adminService;

    private final JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result login(AdminLoginDTO adminLoginDTO) {
        log.info("管理员登录{}", adminLoginDTO.getAccount());
        //登录
        Admin admin = adminService.login(adminLoginDTO);

        //
        Map<String, Object> claim = new HashMap<>();
        claim.put(JwtClaimsConstant.ROLE, JwtClaimsConstant.ADMIN);
        claim.put(JwtClaimsConstant.ACCOUNT, adminLoginDTO.getAccount());
        //生成jwt令牌
        String jwt = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claim);
        return Result.success(jwt);
    }
}
